#include <QFileDialog>
#include <QMessageBox>
#include <QSettings>
#include "DataModel.h"
#include "DlgServerConTest.h"
#include "DlgServerCon.h"
#include "SrvConnectionHttp.h"
#include "SrvConnectionLocal.h"
#include "scImpl.h"

#define FCT "DlgServerCon::DlgServerCon"
DlgServerCon::DlgServerCon(QWidget *parent) : QDialog(parent)
{
	ui.setupUi(this);
	QSettings settings;
	bool local(settings.value("server/isLocal").toBool());
	ui.modeServer->setChecked(!local);
	ui.modeLocal->setChecked(local);
	connect(ui.modeServer, SIGNAL(toggled(bool)), this, SLOT(setAccessSl()));
	connect(ui.modeLocal, SIGNAL(toggled(bool)), this, SLOT(setAccessSl()));
	ui.dbFile->setText(settings.value("server/localDb").toString());
	ui.serverIp->setText(settings.value("server/host").toString());
	ui.serverPort->setText(settings.value("server/port").toString());
	ui.useHttps->setChecked(settings.value("server/useHttps").toBool());
	connect(ui.test, SIGNAL(clicked()), this, SLOT(testSl()));
	setAccessSl();
}

#undef FCT
#define FCT "DlgServerCon::~DlgServerCon"
DlgServerCon::~DlgServerCon()
{
	ctrc(Trc::SETUP,APP,FCT)<<endl;
}

#undef FCT
#define FCT "DlgServerCon::accept"
void DlgServerCon::accept ()
{
	QSettings settings;
	bool restart(false);
	restart|=settings.value("server/isLocal").toBool()!=ui.modeLocal->isChecked();
	settings.setValue("server/isLocal", ui.modeLocal->isChecked());
	restart|=settings.value("server/localDb").toString()!=ui.dbFile->text();
	settings.setValue("server/localDb", ui.dbFile->text());
	restart|=settings.value("server/host").toString()!=ui.serverIp->text();
	settings.setValue("server/host", ui.serverIp->text());
	restart|=settings.value("server/port").toInt()!=ui.serverPort->text().toInt();
	settings.setValue("server/port", ui.serverPort->text().toInt());
	restart|=settings.value("server/useHttps").toBool()!=ui.useHttps->isChecked();
	settings.setValue("server/useHttps", ui.useHttps->isChecked());
	settings.sync();
	if(restart)
		QMessageBox::information(this, tr("Restart Information"), tr("You have to restart the client for these changes to take effect"));
	QDialog::accept();
}

#undef FCT
#define FCT "DlgServerCon::testSl"
void DlgServerCon::testSl()
{
	ctrc(Trc::WORK,APP,FCT)<<"start"<<endl;
	auto_ptr<SrvConnection> testSrv(createSrvConnection());
	testSrv->setVerbose(true);
	DlgServerConTest dlg(*testSrv, false, this);
	dlg.exec();
}

#undef FCT
#define FCT "DlgServerCon::createSrvConnection"
SrvConnection * DlgServerCon::createSrvConnection()
{
	if(ui.modeLocal->isChecked())
		return new SrvConnectionLocal(ui.dbFile->text());
	else
		return new SrvConnectionHttp(ui.serverIp->text(), ui.serverPort->text().toInt(), ui.useHttps->isChecked());
}

#undef FCT
#define FCT "DlgServerCon::setAccessSl"
void DlgServerCon::setAccessSl()
{
	bool isServer(ui.modeServer->isChecked());
	ui.tabWidget->setTabEnabled(1, isServer);
	ui.tabWidget->setTabEnabled(2, isServer);
	//ui.serverIp->setEnabled(isServer);
	//ui.serverPort->setEnabled(isServer);
	//ui.useHttps->setEnabled(isServer);
	bool isLocal(ui.modeLocal->isChecked());
	ui.tabWidget->setTabEnabled(0, isLocal);
	//ui.dbFile->setEnabled(isLocal);
	if(isLocal)
		ui.tabWidget->setCurrentIndex(0);
	else
		ui.tabWidget->setCurrentIndex(1);
}
